package day_03.second;

public class MaxHeap2 {
    private int count;
    private int capacity;
    private int[] data;

    public MaxHeap2(int capacity) {
        this.capacity = capacity;
        this.count = 0;
        this.data = new int[capacity + 1];
    }

    private void shiftUp(int k){
        while(k > 1 && data[k] > data[k/2]){
            swap(data, k, k/2);
            k /= 2;
        }
    }

    private void shiftDown(int k){
        while(2 * k < count){
            int j = 2 * k;
            if(j + 1 <= count && data[j] < data[j+1]){
                j++;
            }
            if(data[k] > data[j]){
                break;
            }
            swap(data, k, j);
            k = j;
        }
    }

    private void swap(int[] data, int k, int i) {
        int tmp = data[k];
        data[k] = data[i];
        data[i] = tmp;
    }
}
